<template>
    <div class="shoppingSet">
        <Row>
            <Col span="24">
                <h2 class="ivu-modal-header">购物设置</h2>
                <div class="mt-10">   
                   <Form ref="shoppingRef" :model="shoppingForm" :rules="shoppingRule" :label-width=150>
                       <Form-item label="是否开启自动确认收货" prop="autoReceiptConfirm" class="conventional-form-item">
                            <i-switch v-model="shoppingForm.autoReceiptConfirm" size="large" @on-change="changeConfirm">
                                <span slot="open">开启</span>
                                <span slot="close">关闭</span>
                            </i-switch>
                        </Form-item>
                        <Form-item label="是否开票" prop="isInvoice">
                            <i-switch v-model="shoppingForm.isInvoice" size="large" @on-change="changeIsInvoice">
                                <span slot="open">开启</span>
                                <span slot="close">关闭</span>
                            </i-switch>
                        </Form-item>
                        <Form-item label="开票内容选项" prop="invoiceCategory">
                                <Input v-if="shoppingForm.isInvoice == true"  style="width: 300px"
                                type="textarea" v-model="shoppingForm.invoiceCategory" placeholder="开票内容,内容用逗号隔开，如xxx,xxx"/>
                        </Form-item>
                        <Form-item label="减库存机制" prop="destockingMechanism" class="conventional-form-item">
                            <Radio-group v-model="shoppingForm.destockingMechanism" @on-change="changeMechanism">
                                    <Radio label="1">
                                        <span>下单时</span>
                                    </Radio>
                                    <Radio label="2">
                                        <span>发货时</span>
                                    </Radio>
                            </Radio-group>
                        </Form-item>
                        <Form-item label="增加销量的时机" prop="salesCountIncrease" class="conventional-form-item">
                            <Radio-group v-model="shoppingForm.salesCountIncrease" @on-change="changeTiming">
                                    <Radio label="1">
                                        <span>发货时</span>
                                    </Radio>
                                    <Radio label="2">
                                        <span>付款时</span>
                                    </Radio>
                            </Radio-group>
                        </Form-item>
                   </Form>
                </div>
            </Col>
            <Col span="24" class="ta-c">
                <Button type="primary" @click.native="submitBasic('shoppingRef')">提交</Button>
                <Button @click.native="closeBasic">取消</Button>
            </Col>
        </Row>
        <Spin size="large" fix v-show="showSpin">
            请稍后....
        </Spin>
    </div>
</template>
<script>
export default {
    name: 'shoppingSet',
    props: ['shopIds'],
    data () {
        return {
            showSpin: false,
            shopId: '',
            shoppingForm: {
                // minOrderPrice: '', // 最小购物金额
                autoReceiptConfirm: true, // 开启自动确认收货
                isInvoice: false, // 是否开票
                invoiceCategory: '',
                destockingMechanism: '', // 减库存机制
                salesCountIncrease: '' // 增加销量的时机
            },
            shoppingRule: {
                autoReceiptConfirm: [],
                invoiceCategory: [
                    {message: '请输入开票内容', trigger: 'blur' },
                    {pattern: /^[\u4e00-\u9fa5a-zA-Z\d,\n]*$/, message: '请输入汉字，中英文及回车符', trigger: 'blur'}
                ]
            }
        };
    },
    mounted () {
        this.shopId = this.$route.query.id;
        this.findShop();
    },
    methods: {
        findShop () {
            depot.get({
                url: this.URL.EC_SELLER.SHOP_FIND_ID + '?id=' + this.shopId,
                cb: (data) => {
                    if (data.code === 0) {
                        this.shoppingForm = data.data;
                        if (this.shoppingForm.destockingMechanism === 1) {
                            this.shoppingForm.destockingMechanism = '1';
                        } else {
                        };
                        if (this.shoppingForm.salesCountIncrease === 1) {
                            this.shoppingForm.salesCountIncrease = '1';
                        } else {
                            this.shoppingForm.salesCountIncrease = '2';
                        };
                        if (this.shoppingForm.autoReceiptConfirm) {
                            this.shoppingForm.autoReceiptConfirm = true;
                        } else {
                            this.shoppingForm.autoReceiptConfirm = false;
                        }
                        if (this.shoppingForm.isInvoice) {
                            this.shoppingForm.isInvoice = true;
                        } else {
                            this.shoppingForm.isInvoice = false;
                        }
                        console.log(this.shoppingForm, 'conventionalFormconventionalForm');
                    }
                },
                ce: (error) => {
                    this.$Message.error(data.message);
                }
            });
        },
        // 是否开启自动确认收货
        changeConfirm (status) {
            this.shoppingForm.autoReceiptConfirm = status;
        },
        // 是否开票
        changeIsInvoice (status) {
            this.shoppingForm.isInvoice = status;
            //  isInvoice
        },
        // 减库存机制
        changeMechanism (val) {
            console.log(val);
            if (val == '1') {
                this.shoppingForm.destockingMechanism = '1';
            } else if (val == '2') {
                this.shoppingForm.destockingMechanism = '2';
            }
        },
        // 增加销量的时机
        changeTiming (val) {
            if (val == '1') {
                this.shoppingForm.salesCountIncrease = '1';
            } else if (val == '2') {
                this.shoppingForm.salesCountIncrease = '2';
            }
        },
        // 提交商城基础设置
        submitBasic (name) {
            // this.showSpin = true;
            console.log(this.shoppingForm.invoiceCategory, 'ssss');
            // 常规设置
            this.$refs[name].validate((valid) => {
                let vue = this;
                vue.shoppingForm.shopId = this.shopId;
                if (valid) {
                    this.showSpin = true;
                    depot.post({
                        url: vue.URL.EC_SELLER.UPDATE_SHOP_SETTINGS,
                        data: vue.shoppingForm,
                        cb: (data) => {
                            // this.showSpin = false;
                            if (data.code === 0) {
                                this.$Message.success('提交成功');
                                this.showSpin = false;
                                this.findShop();
                            } else {
                                // this.$Message.error(data.message);
                                this.showSpin = false;
                            }
                        }
                    });
                } else {
                    this.$Message.error('请检查输入的内容是否有误');
                }
            });
        },
        // 取消提交商城基础设置
        closeBasic () {

        }
    },
    watch: {
        shopIds (newVal, oldVal) {
            if (newVal) {
                this.shopId = newVal;
                this.findShop();
            }
            console.log(this.shopId, '店铺备注设置切换');
        }
    }
};
</script>
<style lang="less" scoped>
    .shoppingSet{

    }
</style>


